/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package midlet;

import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.lwuit.Button;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.GridLayout;
import control.PacWarriorApp;

/**
 * @author Thiago
 */
public class MenuLwuitMidlet extends MIDlet implements ActionListener {
	private String[] img;
	private String[] label;
	private Form menu;
        private PacWarriorApp gameCanvas;
        private Thread thread;
        private Display display;

	public MenuLwuitMidlet() {
		img = new String[5];
		label = new String[5];

		try {

			img[0] = "/battle.png";
			label[0] = "Jogar";

			img[1] = "/highscores.png";
			label[1] = "Ranking";

			img[2] = "/papiro.png";
			label[2] = "Instruções";

                        img[3] = "/escudo.png";
			label[3] = "Sobre";

                        img[4] = "/helmet.png";
			label[4] = "Sair";
		} catch (Exception e) {}
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}

	protected void pauseApp() {}

	protected void startApp() throws MIDletStateChangeException {
		Display.init(this);
		menu = montaMenuForm(label);
		menu.show();
	}

	public Form montaMenuForm(String[] label) {

                Form menu = new Form();
		try {
			menu.setBgImage(Image.createImage("/background.png"));
		} catch (IOException e) {}

		Button b = null;
		int cols = 1;
		int rows = label.length;
		menu.setLayout(new GridLayout(rows,cols));

                for ( int i = 0; i < label.length; i++ ) {
			b = new Button(label[i]);
			b.getStyle().setBgTransparency(1);
                        b.getStyle().setBgColor(120);
			b.setAlignment(Button.CENTER);
			b.setTextPosition(Button.BOTTOM);
                        try{
                            Image icone = Image.createImage(img[i]);
                            b.setIcon(icone);
                        }catch(Exception e){e.printStackTrace();}

			b.setBorderPainted(false);
                        b.addActionListener(this);
			menu.addComponent(b);
		}
		return menu;
	}

	public void actionPerformed(ActionEvent evt) {

                Button b = (Button) evt.getSource();

                if ( b.getText().equals("Jogar")) {
                        this.gameCanvas = new PacWarriorApp();
                        this.thread = new Thread(gameCanvas);
                        thread.start();
			javax.microedition.lcdui.Display.getDisplay(this).setCurrent(gameCanvas);
		} else if ( b.getText().equals("Ranking")) {
			Alert a = new Alert("Selecionado", "Highscores...", null, AlertType.INFO);
			javax.microedition.lcdui.Display.getDisplay(this).setCurrent(a);
		} else if ( b.getText().equals("Instruções")) {
			Alert a = new Alert("Selecionado", "Instruções do jogo...", null, AlertType.INFO);
			javax.microedition.lcdui.Display.getDisplay(this).setCurrent(a);
		} else if ( b.getText().equals("Sobre")){
			Alert a = new Alert("Selecionado", "detalhes da versão...", null, AlertType.INFO);
			javax.microedition.lcdui.Display.getDisplay(this).setCurrent(a);
		} else if ( b.getText().equals("Sair")){
                        this.notifyDestroyed();
		}

                menu.setFocused(b);
	}
}
